package com.zjl.legendary.controller;

import com.zjl.legendary.dto.QuestionDto;
import com.zjl.legendary.entity.Question;
import com.zjl.legendary.entity.User;
import com.zjl.legendary.service.intf.QuestionService;
import com.zjl.legendary.util.BeanUtils;
import com.zjl.legendary.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author zhoujingluo
 * @date 2021/6/14 11:25
 */
@Controller
@RequestMapping("/api/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 批量导入试题
     * @param source 上传的文件资源
     * @param req 请求
     * @return 响应结果
     * @throws IOException 声明IO异常
     */
    @RequestMapping("/upload.do")
    public String upload(MultipartFile source, HttpServletRequest req) throws IOException {
        //从session域中获取用户登录状态
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        //未登录则跳转到登录页面
        if (user == null){
            return "redirect:/login.jsp";
        }
        try {
            //文件上传成功
            if (questionService.saveBatch(source,user.getId())){
                return "/uploadsuccess.jsp";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //文件上传失败
        return "/uploadfail.jsp";
    }

    /**
     * 展示题库
     * @param req 请求
     * @return 响应结果
     */
    @RequestMapping("/showAll.do")
    public String showAll(HttpServletRequest req){
        //获取试题库集合
        List<QuestionDto> qlist = questionService.query();

        String uid = req.getParameter("uid");
        if (StrUtils.isNotNull(uid)){
            qlist = questionService.query(Integer.valueOf(uid));
        }
        //存入request域对象中
        req.setAttribute("qlist",qlist);
        //转发到题库展示页面
        return "forward:/safety/questionlist.jsp";
    }

    /**
     * 模糊查询试题
     * @param req 请求
     * @return 响应结果
     */
    @RequestMapping("/query.do")
    public String query(HttpServletRequest req){
        //根据标题模糊查询试题集合
        List<QuestionDto> qlist = questionService.query(req.getParameter("title"));
        //存入request域对象中
        req.setAttribute("qlist",qlist);
        //转发到题库展示页面
        return "forward:/safety/questionlist.jsp";
    }

    /**
     * 新增试题
     * @param req 请求
     * @return 响应结果
     */
    @RequestMapping("/add.do")
    public String add(HttpServletRequest req){
        //获取session域中存储的用户信息
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        //未登录则跳转到登录页面
        if (user == null){
            return "redirect:/login.jsp";
        }
        //收参并封装试题对象
        Question question = BeanUtils.parseBean(req, Question.class);
        //将试题关联给用户
        question.setUid(user.getId());
        //添加试题
        if (questionService.save(question)){
            //试题添加成功，重定向到题库展示页面
            return "redirect:/api/question/showAll.do";
        } else{
            //试题添加失败重定向到添加试题页面
            return "redirect:/safety/questionadd.jsp";
        }
    }

    /**
     * 为新增任务详情页面提供题库列表
     * @param req 请求
     * @return 响应结果
     */
    @RequestMapping("/showForTask.do")
    public String showForTask(HttpServletRequest req){
        //获取题库试题
        List<QuestionDto> qlist = questionService.query();
        //存入request域
        req.setAttribute("qlist",qlist);
        //转发到任务详情页面
        return "forward:/safety/taskitemadd.jsp";
    }
}
